﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RealState.BusinessObjects;
using RealState.BLL;
using log4net;

namespace RealState.W.uc
{
    public partial class Search : System.Web.UI.UserControl
    {
        private CitiesBLL citiesBLL;
        private TradeTypesBLL tradeTypeBLL;
        private ContentTypesBLL contentTypeBLL;
        private CategoriesBLL categoryBLL;
        private List<VwContents> listContent;

        protected ILog log = LogManager.GetLogger("Search");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GetTradeType();
                GetCategories();
                GetDistricts();
                GetCities();
            }
        }
        private void GetTradeType()
        {
            tradeTypeBLL = new TradeTypesBLL();
            try
            {
                drdTradeTypes.DataSource = tradeTypeBLL.GetTradeTypess();
                drdTradeTypes.DataTextField = "TITLE";
                drdTradeTypes.DataValueField = "ID";
                drdTradeTypes.DataBind();
                drdTradeTypes.Items.Insert(0, new ListItem("-- Chọn ngành nghề--", "0"));
            }
            catch (Exception ex)
            {
                log.Debug(ex.Message);
            }
            finally
            {
                tradeTypeBLL = null;
            }
        }
        private void GetCategories()
        {
            categoryBLL = new CategoriesBLL();
            try
            {
                drdCategories.DataSource = categoryBLL.GetCategoriess().Where(c => c.CategoryType == 2 && c.Deep == 2);
                drdCategories.DataTextField = "TITLE";
                drdCategories.DataValueField = "ID";
                drdCategories.DataBind();
                drdCategories.Items.Insert(0, new ListItem("-- Chọn danh mục --", "0"));
            }
            catch (Exception ex)
            {
                log.Debug(ex.Message);
            }
        }

        private void GetCities()
        {
            citiesBLL = new CitiesBLL();
            try
            {
                drdCity.DataSource = citiesBLL.GetCitiess();
                drdCity.DataTextField = "CityName";
                drdCity.DataValueField = "ID";
                drdCity.DataBind();
                drdCity.Items.Insert(0, new ListItem("-- Thành phố --", "0"));
            }
            catch (Exception ex)
            {
                log.Debug(ex.Message);
            }
            finally
            {
                citiesBLL = null;
            }
        }
        private void GetDistricts()
        {
            DistrictsBLL districtBLL = new DistrictsBLL();
            try
            {
                drdDistrict.DataSource = districtBLL.GetDistrictss();
                drdDistrict.DataTextField = "DistrictName";
                drdDistrict.DataValueField = "ID";
                drdDistrict.DataBind();
                drdDistrict.Items.Insert(0, new ListItem("-- Quận huyện --", "0"));
            }
            catch (Exception ex)
            {
                log.Debug(ex.Message);
            }
            finally
            {
                districtBLL = null;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtSearch.Text) && drdCategories.SelectedValue.Equals("0") && drdCity.SelectedValue.Equals("0") && drdPrice.SelectedValue.Equals("0") && drdTradeTypes.SelectedValue.Equals("0") && drdDistrict.SelectedValue.Equals("0"))
            {
                ShowMsg("Bạn phải nhập từ khóa tìm kiếm hoặc lựa chọn tiêu chí ở trên.");
                return;
            }
            if (drdCategories.SelectedValue.Equals("0"))
            {
                ShowMsg("Bạn phải chọn danh mục muốn tìm");
                return;
            }

            VwContents vcontent = new VwContents();
            vcontent.CategoryId = Convert.ToInt32(drdCategories.SelectedValue);
            vcontent.CityId = Convert.ToInt32(drdCity.SelectedValue);
            vcontent.DistrictID = Convert.ToInt32(drdDistrict.SelectedValue);
            vcontent.Title = txtSearch.Text;
            vcontent.TradeType = drdTradeTypes.SelectedItem.Text;
            vcontent.Price = Convert.ToInt32(drdPrice.SelectedValue);
            VwContentsBLL vcontentsBLL = new VwContentsBLL();
            try
            {
                if (vcontent.Id != 8)
                    listContent = vcontentsBLL.GetViewContents(vcontent).Where(v => v.CategoryId == vcontent.Id).ToList();
                else
                    listContent = vcontentsBLL.GetViewContents(vcontent).Where(v => v.CategoryId == 9 || v.CategoryId == 10).ToList();
            }
            catch (Exception ex)
            {
                log.Debug(ex.Message);
            }
            finally
            {
                vcontentsBLL = null;
            }
           
        

        }
        public static void ShowMsg(string message)
        {
            // Cleans the message to allow single quotation marks 
            string cleanMessage = message.Replace("'", "\'");
            string script = "<script type='text/javascript'>alert('" + cleanMessage + "');</script>";
            // Gets the executing web page 
            Page page = HttpContext.Current.CurrentHandler as Page;
            // Checks if the handler is a Page and that the script isn't allready on the Page 
            if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("alert"))
            {
                page.ClientScript.RegisterClientScriptBlock(typeof(Search), "alert", script);
            }
        }
    }
}